package com.pdx.shiro;

import com.pdx.shiro.service.impl.SecurityServiceImpl;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

/**
 * @author 派 大 星
 * @function 声明自定义Realm
 * @date 2022/4/5 16:49
 * @website https://blog.csdn.net/Gaowumao
 */
public class CustomRealm extends AuthorizingRealm {

    /**
     * 授权
     * @param principals
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        return null;
    }

    /**
     * 认证
     * @param token
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        //获取用户名
        String loginName = (String) token.getPrincipal();
        SecurityServiceImpl securityService = new SecurityServiceImpl();
        String password = securityService.findPassWordByLoginName(loginName);
        if ("".equals(password) || password == null){
            throw new UnknownAccountException("账户不存在");
        }
        return new SimpleAuthenticationInfo(loginName,password,this.getName());
    }
}
